package com.lagou.edu.timer;

import com.lagou.edu.pojo.ServiceNodeInfo;
import com.lagou.edu.zookeeper.ZookeeperFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 赵虎
 * @date 2020/11/30 12:27 上午
 */

public class ReportServer implements Runnable {

    private final SimpleDateFormat dateFormat;

    private final String servicePath;

    public ReportServer(String servicePath) {
        this.servicePath = servicePath;
        this.dateFormat = new SimpleDateFormat("HH:mm:ss");
    }

    @Override
    public void run() {
        System.out.println("定时任务:" + dateFormat.format(new Date()));
        ZookeeperFactory zookeeperFactory = ZookeeperFactory.getInstance();
        try {
            List<ServiceNodeInfo> childNodeValue = zookeeperFactory.getChildNodeValue(servicePath);

            for (ServiceNodeInfo serviceNodeInfo : childNodeValue) {
                if (System.currentTimeMillis() - serviceNodeInfo.getRespDate().getTime() <= 5000L) {
                    continue;
                }
                serviceNodeInfo.setUseTime(0L);
                serviceNodeInfo.setRespDate(new Date());
                zookeeperFactory.updateServiceValue(serviceNodeInfo.getPath(), serviceNodeInfo);
                System.out.println("删除节点类容:" + serviceNodeInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
